﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class PeopleContactBO : ChurchDataBusinessBase
    {
        /// <summary>
        /// There should never be more than one PeopleContact record per person
        /// </summary>
        public PeopleContact GetRecordByPeoplePK(int peoplePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            IEnumerable<PeopleContact> result = from pc in oDB.PeopleContacts
                                                where pc.PeopleFK == peoplePK
                                                select pc;

            if (result.Count() > 0)
                return result.First();
            else
                return null;
        }

        public PeopleContact AddRecord(PeopleContact drPC)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            oDB.PeopleContacts.InsertOnSubmit(drPC);
            oDB.SubmitChanges();
            return drPC;
        }

        public PeopleContact UpdateRecord(PeopleContact drPC)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            PeopleContact dbPC = (from pc in oDB.PeopleContacts
                                  where pc.PeopleContactPK == drPC.PeopleContactPK
                                  select pc).First();

            dbPC.HomePhone = drPC.HomePhone;
            dbPC.CellPhone = drPC.CellPhone;
            dbPC.WorkPhone = drPC.WorkPhone;
            dbPC.PersonalEmail = drPC.PersonalEmail;
            dbPC.FamilyEmail = drPC.FamilyEmail;
            dbPC.PreferredContactMethod = drPC.PreferredContactMethod;
            dbPC.PrimaryAddressFK = drPC.PrimaryAddressFK;
            dbPC.AlternativeAddressFK = drPC.AlternativeAddressFK;
            dbPC.LastUpdTimestamp = DateTime.Now;
            dbPC.LastUpdUser = UserInformation.UserName;
            dbPC.EBulletinInd = drPC.EBulletinInd;
            dbPC.EBulletinSignupDate = drPC.EBulletinSignupDate;
            dbPC.EBulletinUnsubscribeDate = drPC.EBulletinUnsubscribeDate;
            
            oDB.SubmitChanges();
            return drPC;
        }
    }
}
